//
//  RankTopView.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/25.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "RankTopView.h"

@implementation RankTopView{
    
}

- (instancetype)init{
    if (self=[super init]) {
        
    }
    return self;
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr=dataArr;
    for (int i=0; i<dataArr.count; i++) {
        if (i==0) {
            UIView *view=[self viewWithModel:dataArr[i]];
            view.tag=0;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.top.bottom.equalTo(self);
                make.width.mas_equalTo(@(SCREEN_WIDTH/3.0));
            }];
        }else if (i==1){
            UIView *view=[self viewWithModel:dataArr[i]];
            view.tag=1;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.equalTo(self);
                make.width.mas_equalTo(@(SCREEN_WIDTH/3.0));
            }];
        }else if (i==2){
            UIView *view=[self viewWithModel:dataArr[i]];
            view.tag=2;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.top.bottom.equalTo(self);
                make.width.mas_equalTo(@(SCREEN_WIDTH/3.0));
            }];
        }else{
            break;
        }
    }
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(@.7);
    }];
}

- (UIView *)viewWithModel:(RankingUserModel *)model{
    UIView *view=[[UIView alloc]init];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickTap:)];
    [view addGestureRecognizer:tap];
    
    UIImageView *headImage=[[UIImageView alloc]init];
    headImage.layer.masksToBounds=YES;
    headImage.layer.cornerRadius=30;
    headImage.layer.borderColor=DEFAULTORANGECOLOR.CGColor;
    headImage.layer.borderWidth=1;
    [headImage sd_setImageWithURL:[NSURL URLWithString:model.Avater] placeholderImage:[UIImage imageNamed:@"avatar"]];
    [view addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).with.offset(10);
        make.centerX.equalTo(view);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    UIImageView *imageRank=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"ranking_%ld",model.Rank]]];
    [view addSubview:imageRank];
    [imageRank mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(headImage);
        make.centerX.equalTo(headImage.mas_right);
        make.size.mas_equalTo(CGSizeMake(19, 27));
    }];
    
    QXLable *labName=[[QXLable alloc]initTitle];
    labName.text=model.NickName;
    labName.textColor=DEFAULTORANGECOLOR;
    [view addSubview:labName];
    [labName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headImage.mas_bottom).with.offset(13);
        make.centerX.equalTo(headImage);
    }];

    
    UIButton *btneExceptional =[[UIButton alloc]init];
    if (self.type==RankTypeFans) {
        [btneExceptional setImage:[UIImage imageNamed:@"fans"] forState:UIControlStateNormal];
    }else{
        [btneExceptional setImage:[UIImage imageNamed:@"master_integral"] forState:UIControlStateNormal];
    }
    
    
    [btneExceptional setTitleColor:DEFAULTORANGECOLOR forState:UIControlStateNormal];
    [btneExceptional setTitle:[NSString stringWithFormat:@" %ld",model.Number] forState:UIControlStateNormal];
    [btneExceptional.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [view addSubview:btneExceptional];
    [btneExceptional mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headImage);
        make.top.equalTo(labName.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    
    return view;
}

- (void)onclickTap:(UITapGestureRecognizer *)tap{

    if (self.selectBlock) {
        self.selectBlock(tap.view.tag);
    }
}

@end
